package com.tim.util;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SqlAnalysisUtil {
	
	/**
	 * 按sql行，生成获取枚举类名 <br/>
	 * 	`reward_type` varchar(10) NOT NULL COMMENT '奖励类型 POINTS-积分 COUPON-优惠券', => RewardTypeEnum
	 */
	public static String getEnumNameByDbLine(String dbLine) {
		String className =  dbLine.substring(0, dbLine.indexOf(" ")).replaceAll(classNameRemoveREG, "").trim();
		//首字母转大写
		className = className.replaceFirst(className.substring(0, 1), className.toUpperCase().substring(0, 1));
		//下划线转大写
		if(className.contains("_")) {
			String regex = "(_[a-z])";
			Matcher matcher = Pattern.compile(regex).matcher(className);
			while(matcher.find()) {
				String group = matcher.group(1);
				className = className.replace(group, group.toUpperCase().substring(1));
			}
		}
//		System.out.println(className);
		className = className+"Enum";
		return className;
	}

	/**
	 * 获取sql行第一个flag内内容
	 */
	public static String getFirstQuoteContent(String dbLine, String flag) {
		int start = dbLine.indexOf(flag);
		int end = dbLine.indexOf(flag, ++start);
		if(start<0 || end<0) {
			return "";
		}		
		return dbLine.substring(start, end);
	}
	/**
	 * 获取sql行第一个flag内内容
	 */
	public static String getFirstQuoteContent(String dbLine) {
		return getFirstQuoteContent(dbLine, "`");
	}
	/**
	 * 按sql行获取字段类型、名称等信息
	 */
	public static String[] getFieldInfos(String dbLine) {
		String[] strs = dbLine.trim().split("\\s+");
		String field = getFirstQuoteContent(dbLine);
		String type = toJavaType(strs[1]);
		String comment = getComment(dbLine);
		return new String[] {StringAnalysisUtil.toCamel(field), field, type, comment};
	}
	/**
	 * 获取sql行注释内容
	 */
	public static String getComment(String dbLine) {
		String comment = "";
		int flag = dbLine.indexOf(sqlCommentStr);
		if(flag > -1) {
			comment = dbLine.substring(flag+sqlCommentStr.length(), dbLine.length()-1)
					.replaceAll(classNameRemoveREG, "").trim();
		}
		if(comment.startsWith("=")) {
			comment = comment.substring(1);
		}
		return comment;
	}
	
	/**
	 * sql类型转java类型
	 */
	public static String toJavaType(String dbType) {
		dbType = dbType.toLowerCase();
		if(dbType.contains("tinyint(")) {
			return "Byte";
		}else if(dbType.contains("bigint(")) {
			return "Long";
		}else if(dbType.contains("int(")) {
			return "Integer";
		}else if(dbType.contains("time")) {
			return "Date";
		}else if(dbType.contains("decimal(")) {
			return "BigDecimal";
		}else {
			return "String";
		}
	}

	/**
	 * 包装sql关键词
	 */
	public static String quoteKeyword(String colName) {
		colName = colName.trim();
		return keywords.contains(colName) ? "`"+colName+"`" : colName;
	}


	/**sql生成类名时移除内容*/
	public static String classNameRemoveREG = "['\"`]";
	/**sql生成字段名时移除内容*/
	public static String fieldDescRemoveREG = "['\"`\\、,，:：\\.=-]";
	/**sql注释标志位*/
	public static String sqlCommentStr = "COMMENT";
	/**sql关键词*/
	public static List<String> keywords = Arrays.asList("status", "condition");

}
